View Javadoc

1   // Copyright (C) 2004, Brian Enigma <enigma at netninja.com>
2   // This file is part of Mac-Package.
3   //
4   // Mac-Package is free software; you can redistribute it and/or modify
5   // it under the terms of the GNU General Public License as published by
6   // the Free Software Foundation; either version 2 of the License, or
7   // (at your option) any later version.
8   //
9   // Mac-Package is distributed in the hope that it will be useful,
10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  // GNU General Public License for more details.
13  //
14  // You should have received a copy of the GNU General Public License
15  // along with Foobar; if not, write to the Free Software
16  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  package org.ninjasoft.macpackager;
18  
19  import java.io.*;
20  import java.util.*;
21  
22  /***
23   * A simple builder of the Info.plist file.  This could have alternately been
24   * done by loading an XML file and XPathing various results (or XSLT), but to
25   * keep dependencies low, basic strings are what we ended up using.
26   * @author Brian Enigma
27   */
28  public InfoBuilder {/package-summary.html">class InfoBuilder {
29      private String bundleName = "unnamed";
30      private String bundleIdentifier = ""; // Calculated result
31      private String version = "1.0.0";
32      private String infoString = "TODO: unnamed, v1.0.0, Copyright(c) 1900";
33      private String vmOptions = "";
34      private String mainClass = "";
35      private Vector jarFiles = new Vector();
36      
37      private static final String CONTENTS = 
38      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
39      "<!DOCTYPE plist SYSTEM \"file://localhost/System/Library/DTDs/PropertyList.dtd\">\n" +
40      "<plist version=\"0.9\">\n" +
41      "<dict>\n" +
42      "    <key>CFBundleName</key>\n" +
43      "    <string>%1</string>\n" +
44      "    <key>CFBundleIdentifier</key>\n" +
45      "    <string>%2</string>\n" +
46      "    <key>CFBundleVersion</key>\n" +
47      "    <string>%3</string>\n" +
48      "    <key>CFBundleAllowMixedLocalizations</key>\n" +
49      "    <string>true</string>\n" +
50      "    <key>CFBundleExecutable</key>\n" +
51      "    <string>JavaApplicationStub</string>\n" +
52      "    <key>CFBundleDevelopmentRegion</key>\n" +
53      "    <string>English</string>\n" +
54      "    <key>CFBundlePackageType</key>\n" +
55      "    <string>APPL</string>\n" +
56      "    <key>CFBundleShortVersionString</key>\n" +
57      "    <string>%3</string>\n" +
58      "    <key>CFBundleSignature</key>\n" +
59      "    <string>????</string>\n" +
60      "    <key>CFBundleGetInfoString</key>\n" +
61      "    <string>%4</string>\n" +
62      "    <key>CFBundleInfoDictionaryVersion</key>\n" +
63      "    <string>6.0</string>\n" +
64      "    <key>CFBundleIconFile</key>\n" +
65      "    <string>icon.icns</string>\n" +
66      "    <key>Java</key>\n" +
67      "    <dict>\n" +
68      "        <key>VMOptions</key>\n" +
69      "        <string>%5</string>\n" +
70      "        <key>MainClass</key>\n" +
71      "        <string>%6</string\n>" +
72      "        <key>JVMVersion</key>\n" +
73      "        <string>1.4+</string>\n" +
74      "        <key>ClassPath</key>\n" +
75      "            <array>\n" +
76      "%7" + 
77      "            </array>\n" +
78      "        <key>Properties</key>\n" +
79      "        <dict>\n" +
80      "            <key>apple.laf.useScreenMenuBar</key>\n" +
81      "            <string>true</string>\n" +
82      "        </dict>\n" +
83      "    </dict>\n" +
84      "</dict>\n" +
85      "</plist>\n";
86      
87      public String getXml() {
88          StringBuffer result = new StringBuffer();
89          int max = CONTENTS.length();
90          calculateBundleIdentifier();
91          for (int i=0; i<max; i++) {
92              char c = CONTENTS.charAt(i);
93              if (c != '%') {
94                  result = result.append(c);
95              } else {
96                  i++;
97                  c = CONTENTS.charAt(i);
98                  if (c=='1') {
99                      result.append(this.bundleName);
100                 } else if (c=='2') {
101                     result.append(this.bundleIdentifier);
102                 } else if (c=='3') {
103                     result.append(this.version);
104                 } else if (c=='4') {
105                     result.append(this.infoString);
106                 } else if (c=='5') {
107                     result.append(this.vmOptions);
108                 } else if (c=='6') {
109                     result.append(this.mainClass);
110                 } else if (c=='7') {
111                     for (Iterator j = this.jarFiles.iterator(); j.hasNext(); ) {
112                         result.append("<string>$JAVAROOT/");
113                         result.append(j.next().toString());
114                         result.append("</string>\n");
115                     }
116                 }
117             }
118         }
119         return result.toString();
120     }
121 
122     
123     private void calculateBundleIdentifier() {
124         this.bundleIdentifier = this.mainClass + "." + this.version;
125     }
126     
127     // #######################################################################
128     // Getters and setters
129 	/***
130 	 * @return Returns the bundleName.
131 	 */
132 	public String getBundleName() {
133 		return bundleName;
134 	}
135 	/***
136 	 * @param bundleName The bundleName to set.
137 	 */
138 	public void setBundleName(String bundleName) {
139 		this.bundleName = bundleName;
140 	}
141 	/***
142 	 * @return Returns the infoString.
143 	 */
144 	public String getInfoString() {
145 		return infoString;
146 	}
147 	/***
148 	 * @param infoString The infoString to set.
149 	 */
150 	public void setInfoString(String infoString) {
151 		this.infoString = infoString;
152 	}
153 	/***
154 	 * @param jarFiles The jarFiles to set.
155 	 */
156 	public void addJarFile(String filename) {
157         File f = new File(filename);
158         this.jarFiles.add(f.getName());
159 	}
160 	/***
161 	 * @return Returns the mainClass.
162 	 */
163 	public String getMainClass() {
164 		return mainClass;
165 	}
166 	/***
167 	 * @param mainClass The mainClass to set.
168 	 */
169 	public void setMainClass(String mainClass) {
170 		this.mainClass = mainClass;
171 	}
172 	/***
173 	 * @return Returns the version.
174 	 */
175 	public String getVersion() {
176 		return version;
177 	}
178 	/***
179 	 * @param version The version to set.
180 	 */
181 	public void setVersion(String version) {
182 		this.version = version;
183 	}
184 	/***
185 	 * @return Returns the vmOptions.
186 	 */
187 	public String getVmOptions() {
188 		return vmOptions;
189 	}
190 	/***
191 	 * @param vmOptions The vmOptions to set.
192 	 */
193 	public void setVmOptions(String vmOptions) {
194 		this.vmOptions = vmOptions;
195 	}
196 }